package com.hetao.xx.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

import com.hetao.xx.Global;
import com.hetao.xx.R;
import com.hetao.xx.orm.NpcEventVo;
import com.hetao.xx.orm.NpcVo;
import com.hetao.xx.service.FightManager;
import com.hetao.xx.service.PlaceEventService;
import com.hetao.xx.service.PlaceManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class HomeEventFragment extends Fragment {

    private View root;
    private Unbinder unbinder;
    private NpcEventVo event;
    private NpcVo npcVo;

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_event_layout)
    CardView eventLayout;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_event_name)
    TextView name;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_event_content)
    TextView content;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_event_ok)
    Button okBtn;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_event_cancel)
    Button cancelBtn;

    private final Global global = Global.getInstant();
    private final PlaceManager placeManager = global.placeManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (root == null) {
            root = inflater.inflate(R.layout.fragment_home_event, container, false);
            unbinder = ButterKnife.bind(this, root);
            init();
        }
        return root;
    }

    public void init() {
    }

    public void update() {
        npcVo = placeManager.getNpcById(event.npcId);
        if(npcVo == null || TextUtils.isEmpty(npcVo.name)) {
            name.setText("系统:");
        } else {
            name.setText(npcVo.name+":");
        }

        content.setText(event.message);
        okBtn.setVisibility(event.okEvent != null ? View.VISIBLE: View.GONE);
        cancelBtn.setVisibility(event.cancelEvent != null ? View.VISIBLE: View.GONE);

    }

    @SuppressLint("NonConstantResourceId")
    @OnClick({R.id.home_event_ok, R.id.home_event_cancel})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_event_ok:
                clickOkEvent();
                break;
            case R.id.home_event_cancel:
                PlaceEventService.clickCancelEvent(event);
                break;
        }

    }

    private void clickOkEvent() {
        switch (event.eventType){
            case Talk:
                PlaceEventService.clickCancelEvent(event);
                break;
            case SaveArchive:
                break;
            case Fight:
//                new FightManager().fightNpc(npcVo);
                break;
            case GiveTo:
                break;
        }
    }

    public void setEvent(NpcEventVo event) {
        this.event = event;
    }

    @Override
    public void onResume() {
        super.onResume();
        update();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //进行判空，避免空指针
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
